package java_0629;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class CustomerAndProduceModel {
    public static void main(String[] args) throws InterruptedException {
        //使用一个BlockingQueue作为交易场所
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();
        //新建一个线程作为消费者
        Thread customer = new Thread(){
            @Override
            public void run() {
                while(true){
                    try {
                        Integer value = queue.take();
                        System.out.println("消费了元素："+value);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        customer.start();

        //新建一个线程作为生产者
        Thread producer = new Thread(){
            @Override
            public void run() {
                for(int i=1;i<=10000;i++){
                    System.out.println("生产了元素："+i);

                    try {
                        queue.put(i);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        };
        producer.start();

        customer.join();
        producer.join();
    }
}
